<!--
  * 分润提现表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-23 04:08:27
-->
<template>
  <a-modal
    :title="auditFlag ? '代理商提现审核' : '提现审核查看'"
    :width="600"
    :open="visibleFlag"
    @cancel="onClose"
    :maskClosable="false"
    :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form">
      <group-title class="pb-0.5" :iconOptions="{ class: 'text-blue-400' }">基础信息</group-title>
      <a-row>
        <a-col :span="12">
          <a-form-item label="代理商名称" name="bankCode">
            <span>{{ form.agentInfo.companyName }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="登录账号" name="bankBranchCode">
            <span>{{ form.agentInfo.no }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="联系人" name="bankAddressCode">
            <span>{{ form.agentInfo.name }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="联系方式" name="accName">
            <span>{{ form.agentInfo.phone }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="地址" name="accType">
            <span>{{ form.agentInfo.addr }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" name="cardNo">
            <span>{{ $smartEnumPlugin.getDescByValue('ZZB_ENABLE_STATUS_ENUM', form.agentInfo.status) }}</span>
          </a-form-item>
        </a-col>
      </a-row>
      <group-title class="pb-0.5" :iconOptions="{ class: 'text-blue-400' }">结算银行卡-分润</group-title>
      <a-row>
        <a-col :span="12">
          <a-form-item label="结算卡号" name="cardNo">
            <span>{{ form.debitCardInfo.cardNo }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开户人姓名" name="accName">
            <span>{{ form.debitCardInfo.accName }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开户行" name="bankName">
            <span>{{ form.debitCardInfo.bankName }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="法人身份证号码" name="legalPersonId">
            <span>{{ form.debitCardInfo.legalPersonId }}</span>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="12">
          <a-form-item label="联行" name="bankBranchName">
            <span>{{ form.debitCardInfo.bankBranchName }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="开户行地区" name="bankAddressName">
            <span>{{ form.debitCardInfo.bankAddressName }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="结算账户类型" name="accType">
            <span>{{ $smartEnumPlugin.getDescByValue('DEBIT_CARD_ACC_TYPE_ENUM', form.debitCardInfo.debitAccType) }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="结算卡类型" name="cardType">
            <span>{{ $smartEnumPlugin.getDescByValue('CARD_TYPE_ENUM', form.debitCardInfo.cardType) }}</span>
          </a-form-item>
        </a-col> -->
      </a-row>
      <group-title class="pb-0.5" :iconOptions="{ class: 'text-blue-400' }">提现信息</group-title>
      <a-row>
        <a-col :span="8">
          <a-form-item label="期初金额" name="drawAmt">
            <span>{{ form.sAmt }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="提现金额" name="drawAmt">
            <span>{{ form.drawAmt }}</span>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="手续费（1‰）" name="drawAmt">
            <span>{{ form.fee }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="代扣费（7%）" name="drawAmt">
            <span>{{ form.holdFee }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="实际到账金额" name="drawAmt">
            <span>{{ form.eAmt }}</span>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-item label="发票号" name="invoiceCode">
            <span>{{ form.invoiceCode }}</span>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="" name="invoiceCode">
            <a-button type="primary" :href="form.invoiceUrl" :disabled="!form.invoiceUrl">查看电子发票</a-button>
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item label="备注" name="remark">
        <span>{{ form.remark }}</span>
      </a-form-item>
      <a-form-item
        v-if="
          auditFlag &&
          (ZZB_AUDIT_STATUS_ENUM.AUDIT_STATUS_ING.value === form.auditStatus || ZZB_AUDIT_STATUS_ENUM.AUDIT_STATUS_WAIT.value === form.auditStatus)
        "
        label="审核意见"
        name="rejectReason"
      >
        <a-textarea style="width: 100%; color: #1f1f1f" v-model:value="form.rejectReason" />
      </a-form-item>
      <a-form-item
        v-if="
          !auditFlag &&
          (ZZB_AUDIT_STATUS_ENUM.AUDIT_STATUS_REJECT.value === form.auditStatus ||
            ZZB_AUDIT_STATUS_ENUM.AUDIT_STATUS_SUCCESS.value === form.auditStatus)
        "
        label="审核意见"
        name="rejectReason"
      >
        <a-input style="width: 100%; color: #1f1f1f" v-model:value="form.rejectReason" :bordered="false" disabled />
      </a-form-item>
    </a-form>
    <template #footer>
      <a-space
        v-if="
          auditFlag &&
          (ZZB_AUDIT_STATUS_ENUM.AUDIT_STATUS_ING.value === form.auditStatus || ZZB_AUDIT_STATUS_ENUM.AUDIT_STATUS_WAIT.value === form.auditStatus)
        "
      >
        <a-button @click="auditFail">审核不通过</a-button>
        <a-button type="primary" @click="auditSuccess">审核通过</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { ZZB_AUDIT_STATUS_ENUM } from '/@/constants/system/system-const.js'
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
  import { reactive, ref, nextTick, createVNode } from 'vue'
  import _ from 'lodash'
  import { message, Modal } from 'ant-design-vue'
  import { SmartLoading } from '/src/components/framework/smart-loading'
  import { profitDrawApi } from '/@/api/business/b-operation/profit/profit-draw/profit-draw-api'
  import { smartSentry } from '/src/lib/smart-sentry'

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList'])

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false)
  const auditFlag = ref(false)

  function show(rowData, flag) {
    Object.assign(form, formDefault)
    auditFlag.value = flag

    profitDrawApi.findOne({ id: rowData.id }).then((res) => {
      const data = res.data
      data.sAmt = (data.sAmt / 100).toFixed(2)
      data.drawAmt = (data.drawAmt / 100).toFixed(2)
      data.eAmt = (data.eAmt / 100).toFixed(2)
      data.fee = (data.fee / 100).toFixed(2)
      data.holdFee = (data.holdFee / 100).toFixed(2)
      if (data && !_.isEmpty(data)) {
        Object.assign(form, data)
      }
    })

    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true
    nextTick(() => {
      formRef.value.clearValidate()
    })
  }

  function onClose() {
    Object.assign(form, formDefault)
    visibleFlag.value = false
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref()

  const formDefault = {
    id: undefined,
    drawAmt: undefined, // 提现金额
    fee: undefined, // 发票号
    invoiceCode: undefined, // 发票号
    holdFee: undefined, // 发票号
    invoiceUrl: undefined, // 发票地址
    remark: undefined, // 备注
    auditStatus: undefined, // 备注
    rejectReason: undefined, // 备注
    debitCardInfo: {
      bankCode: undefined, // 开户行编号
      bankName: undefined, // 开户行名称
      bankBranchCode: undefined, // 联行号
      bankBranchName: undefined, // 联行名称
      bankAddressCode: undefined, // 开户行地区编码
      bankAddressName: undefined, // 开户行地区名称
      accName: undefined, // 开户人姓名
      debitAccType: undefined, // 结算账户类型
      cardNo: undefined, // 结算卡号
      cardType: undefined, // 结算卡类型
      legalPersonId: undefined, // 法人身份证号码
      defaultedFlag: undefined, // 是否为默认提现账户
    },
    agentInfo: {
      no: undefined,
      companyName: undefined,
      name: undefined,
      phone: undefined,
      addr: undefined,
      status: undefined,
    },
  }

  let form = reactive({ ...formDefault })

  // 点击确定，验证表单
  async function onSubmit(auditFlag) {
    Modal.confirm({
      title: auditFlag ? '审核通过' : '审核不通过',
      icon: createVNode(ExclamationCircleOutlined),
      content: auditFlag
        ? `将通过“${form.agentInfo.companyName}”的提现审核信息，请认真校对！`
        : `将拒绝“${form.agentInfo.companyName}”的提现审核信息，请认真校对！`,
      async onOk() {
        audit(auditFlag)
      },
      onCancel() {},
    })
  }

  // 新建、编辑API
  async function audit(auditFlag) {
    SmartLoading.show()
    try {
      const param = { id: form.id, auditStatus: auditFlag ? 2 : 3, rejectReason: form.rejectReason }
      await profitDrawApi.audit(param)
      message.success('操作成功')
      emits('reloadList')
      onClose()
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }

  async function auditFail() {
    onSubmit(false)
  }

  async function auditSuccess() {
    onSubmit(true)
  }

  defineExpose({
    show,
  })
</script>
<style lang="less" scoped>
  :deep(:where(.css-dev-only-do-not-override-vgohfh).ant-form-item) {
    margin-bottom: 12px;
  }
</style>
